package com.javasea.web.mapstruct.controller;

import com.javasea.web.mapstruct.entity.Order;
import com.javasea.web.mapstruct.entity.User;
import com.javasea.web.mapstruct.entity.UserVo;
import com.javasea.web.mapstruct.mapper.UserMapper;
import com.javasea.web.mapstruct.mapper.UserOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/mapStructToVo")
    public void mapStructToVo() {
        User user = new User();
        user.setId(1).setEmail("1964327885@qq.com").setUserName("小慕").setBirthday(new Date()).setTel("18772563087");
        UserVo userVo = UserMapper.INSTANCE.convertToVo(user);
        System.out.println(userVo);
    }

    @GetMapping("/mapStructToList")
    public void mapStructToList() {
        User user1 = new User().setId(1).setEmail("1964327885@qq.com").setUserName("小慕")
                .setBirthday(new Date()).setTel("18772563087");
        User user2 = new User().setId(2).setEmail("1664687767@qq.com").setUserName("小王")
                .setBirthday(new Date()).setTel("13455332134");
        User user3 = new User().setId(3).setEmail("1323243433@qq.com").setUserName("小张")
                .setBirthday(new Date()).setTel("1534323232");
        List<User> userList = new ArrayList<>();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        List<UserVo> userVoList = UserMapper.INSTANCE.toVoList(userList);
        System.out.println(userVoList);
    }

    /**
     * 组合映射
     * @return
     */
    @GetMapping("/compositeMapping")
    public void compositeMapping() {
        //新建一个user对象
        User user = new User();
        user.setId(1).setEmail("1964327885@qq.com").setUserName("小慕")
                .setBirthday(new Date()).setTel("18772563087");
        //新建一个Order对象
        Order order = new Order();
        order.setReceiverAddress("湖北省武汉市洪山区").setOrderNo("323121213232");
        UserOrderVo userOrderVo = UserMapper.INSTANCE.toUserOrderVo(user,order);
        System.out.println(userOrderVo);
    }

}
